//
//  ViewController.m
//  AppSettings
//
//  Created by Justsoso on 16/3/16.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;


@property (nonatomic,copy) NSString *str;


@end

@implementation ViewController

- (NSString *)str
{
    if (_str == nil)
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        _str = [defaults objectForKey:@"time"];
        if (_str == nil)
        {
            _str = @"";
        }
        
    }
    return _str;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.textView.text = self.str;
    [self changeColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateInfo:)
                                                 name:UIApplicationWillResignActiveNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateInfo:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateInfo:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateInfo:)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
    
}

- (void)changeColor
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    float red = [defaults floatForKey:@"red"];
    float green = [defaults floatForKey:@"green"];
    float blue = [defaults floatForKey:@"blue"];
    self.view.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self changeColor];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)updateInfo:(NSNotification *)noti
{
    NSMutableString *dateStr;
    if (noti.name == UIApplicationWillResignActiveNotification)
    {
         dateStr = [[NSString stringWithFormat:@"%@ 将取消活动\n",[self time]] mutableCopy];
    }
    if (noti.name == UIApplicationDidEnterBackgroundNotification)
    {
        dateStr = [[NSString stringWithFormat:@"%@ 已经进入后台\n",[self time]] mutableCopy];
    }
    if (noti.name == UIApplicationWillEnterForegroundNotification)
    {
        dateStr = [[NSString stringWithFormat:@"%@ 将进入前台\n",[self time]] mutableCopy];
        [self changeColor];
    }
    if (noti.name == UIApplicationDidBecomeActiveNotification)
    {
        dateStr = [[NSString stringWithFormat:@"%@ 已经激活\n",[self time]] mutableCopy];
    }
    
    [dateStr appendString:self.str];
    self.textView.text = dateStr;
    self.str = dateStr;
    [self save];
    
}

- (NSString *)time
{
    NSDate *now = [NSDate date];
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"HH:mm:ss"];
    return [f stringFromDate:now];
}

- (void)save
{
    self.textView.text = self.str;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.str forKey:@"time"];
    [defaults synchronize];
}
@end
